package com.zx._03_并发.chapter_04_public;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import lombok.extern.slf4j.Slf4j;

/**
 * 对象发布——安全发布对象——单例模式2——饿汉模式(线程安全的方式，是安全发布对象的方式)
 */
@Slf4j
public class Demo_03_singleton2 {

    // 单例对象
    private static Demo_03_singleton2 singleton = new Demo_03_singleton2();

    // 私有构造方法
    private Demo_03_singleton2() {
    }

    // 静态工厂方法
    public static Demo_03_singleton2 getInstance() {
        return singleton;
    }

    public static void main(String[] args) {
        ExecutorService pool = Executors.newCachedThreadPool();
        for (int i = 0; i < 20; i++) {
            pool.execute(() -> log.info(Demo_03_singleton2.getInstance().toString()));
        }
        pool.shutdown();
    }
}
